package TwoPointers;

public class _75_SortColors {
    public void sortColors_1(int[] nums) {
        int start = 0, end = nums.length - 1;
        for (int i = 0; i < nums.length; i++) {
            if (start <= end && i < end) {
                if (nums[i] == 0) {
                    int temp = nums[start];
                    nums[start++] = 0;
                    nums[i] = temp;
                } else if (nums[i] == 2) {
                    int temp = nums[end];
                    nums[end--] = 2;
                    nums[i--] = temp;
                }
            } else {
                break;
            }

        }
    }

    public void sortColor(int[] nums) {
        int[] colors = new int[]{0,0,0};
        for (int i = 0; i < nums.length; i++) {
            colors[nums[i]] += 1;
        }
        int pos = 0;
        for (int i = 0; i < colors[0]; i++) {
            nums[pos++] = 0;
        }
        for (int i = 0; i < colors[1]; i++) {
            nums[pos++] = 1;
        }
        for (int i = 0; i < colors[2]; i++) {
            nums[pos++] = 2;
        }
    }
}
